package net.liuyimin.controller;

import net.liuyimin.service.StockService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class StockController {

    @Resource
    private StockService stockService;

    /**
     * 演示超卖现象
     */
    @GetMapping("stock/deduct")
    public String deduct(){
        stockService.deduct();
        return "hello stock deduct！！";
    }

    /**
     * 使用 synchronized 关键字解决超卖现象
     */
    @GetMapping("stock/deductBySynchronized")
    public String deductBySynchronized(){
        stockService.deductBySynchronized();
        return "hello stock deductBySynchronized！！";
    }

    /**
     * 使用 ReentrantLock 可重入锁解决超卖现象
     */
    @GetMapping("stock/deductByReentrantLock")
    public String deductByReentrantLock(){
        stockService.deductByReentrantLock();
        return "hello stock deduct！！";
    }

}
